package fudan.se.project.service.infrastructure.base;


import fudan.se.project.domain.User;
import fudan.se.project.domain.conference.production.Paper;
import fudan.se.project.domain.conference.production.ReviewInfo;
import fudan.se.project.repository.entity.conference.production.PaperRepository;
import fudan.se.project.repository.entity.conference.production.ReviewInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PaperPermissionService {

    private final PaperRepository paperRepository;
    private final ReviewInfoRepository reviewInfoRepository;

    private static final String AUTHOR = "AUTHOR";
    private static final String PCMEMBER = "PCMEMBER";
    private static final String ANY = "ANY";

    @Autowired
    public PaperPermissionService(PaperRepository paperRepository, ReviewInfoRepository reviewInfoRepository) {
        this.paperRepository = paperRepository;
        this.reviewInfoRepository = reviewInfoRepository;
    }


    public boolean checkPermission(User user, String authority, long resourceId) {
        Paper paper = paperRepository.findByAuthorUserAndId(user, resourceId);
        ReviewInfo reviewInfo = reviewInfoRepository.findByPcMemberUserAndPaperId(user, resourceId);
        switch (authority) {
            case AUTHOR:
                return null != paper;
            case PCMEMBER:
                return null != reviewInfo;
            case ANY:
                return null != paper || null != reviewInfo;
            default:
                return false;
        }
    }

    public boolean checkAuthorPermission(User user, Paper paper) {
        return this.checkPermission(user, AUTHOR, paper.getId());
    }


}
